<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/19 0019
 * Time: 9:31
 */
namespace Api\Model;
use Think\Model;

class GoodsClassModel extends Model{

    //获得格式化的信息
    public function getCateTree($id=0){
        $data=$this
            ->field("class_id as oid, name as title,class_icon as img,parent_class_id") //需要加字段
            ->where(array('parent_class_id'=>$id))->select();

        //对获取的信息进行格式化
        $list =$this-> getTree($data);
        //dump($list);die;
        return $list;
    }

    //格式化分类信息  无限极分类
    public function getTree($data ){//为true查询所有数据
        //根据参数决定是否需要重置

        if($data) {
            $list = array();
            foreach ($data as $key => $value) {
                //使用递归方法获取分类下的子分类
                $list[$key] = $value;
                //json_decode($list[$key]['contenta']);
                unset($list[$key]['img']);

                if($value['parent_class_id']==0) {

                    $adverti_class = M('adverti_class')->where("state=1 AND class_name='商品分类_id" . $value['oid'] . "'")->find();
                    $adverti_list = array();
                    if($adverti_class){
                        $adverti_list = M('adverti_list')->where('state=1 AND adverti_class_id='.$adverti_class['adverti_class_id'])->order('sort desc')->limit(3)->select();
                    }
                    $list[$key]['adverti'] = $adverti_list;
                }
                $list[$key]['contenta'] = $this->getCateTree($value['oid']);
            }

            return $list;
        }else{

            return $data;
        }
        //dump($list);die;
    }
}